/**
 * 
 */
package com.jrh.dal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;

import com.jrh.domain.common.Page;
import com.jrh.domain.entity.Product;
import com.jrh.domain.entity.ProductRef;

/**
 * 类ProductDao.java的实现描述：TODO 类实现描述
 * 
 * @author IBM 2012-7-18 下午8:29:32
 */
public class ProductDao extends BaseDao<Product> {

    {
        namespace = "jrh.single.product";
    }

    @SuppressWarnings("unchecked")
    public Page<Product> getProductDetailPageByCommonCondition(Map<String, Object> params, Integer start,
                                                               Integer pageSize) {
        Map<String, Object> map = this.getPageMap(start, pageSize);
        map.putAll(params);
        map.put("start", start);
        map.put("pageSize", pageSize);
        List<Product> productList = (List<Product>) sqlMapClientTemplate.queryForList(namespace
                                                                                              + ".listProductDetailByCondition",
                                                                                      map);
        Integer count = (Integer) sqlMapClientTemplate.queryForObject(namespace + ".listProductDetailByCondition_count",
                                                                      map);
        return new Page<Product>(productList, start, pageSize, count);
    }

    @SuppressWarnings("unchecked")
    public List<Product> listProductDetailByCommonCondition(Map<String, Object> params) {
        List<Product> productList = (List<Product>) sqlMapClientTemplate.queryForList(namespace
                                                                                              + ".listProductDetailByCondition",
                                                                                      params);
        return productList;
    }

    @SuppressWarnings("unchecked")
    public List<Product> listByIds(List<Long> ids) {
        if (!CollectionUtils.isEmpty(ids)) {
            return (List<Product>) sqlMapClientTemplate.queryForList(namespace + ".listByIds", ids);
        }
        return new ArrayList<Product>();
    }

    @SuppressWarnings("unchecked")
    public List<Product> listProductByCondition(Long brandId, String name) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("brandId", brandId);
        map.put("name", name);
        return (List<Product>) sqlMapClientTemplate.queryForList(namespace + ".listProductByCondition", map);
    }

    public boolean updateProductBySale(Product product) {
        return sqlMapClientTemplate.update(namespace + ".updateProductBySale", product) != 0;
    }

    public boolean updateProductByUser(Product product) {
        return sqlMapClientTemplate.update(namespace + ".updateProductByUser", product) != 0;
    }

    @SuppressWarnings("unchecked")
    public List<Product> listProductByProductRef(Long refId, String refType) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("refId", refId);
        map.put("refType", refType);
        return (List<Product>) sqlMapClientTemplate.queryForList(namespace + ".listProductByProductRef", map);
    }
}
